package net.gazhi.delonix.grac.service;

import net.gazhi.delonix.core.entity.LoginUser;
import net.gazhi.delonix.core.service.AbstractEntityService;
import net.gazhi.delonix.core.thread.ThreadContext;
import net.gazhi.delonix.grac.AC_TB;
import net.gazhi.delonix.grac.entity.gru.User;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class UserService extends AbstractEntityService<User> {

	/**
	 * 根据用户名查找用户
	 * 
	 * @param loginName
	 * @return
	 */
	public User findByLoginName(String loginName) {
		return (User) dao.createCriteria(User.class).add(Restrictions.eq("loginName", loginName)).setMaxResults(1).uniqueResult();
	}

	/**
	 * 修改用户语言
	 * @param locale
	 */
	@Transactional
	public void switchLocale(String locale) {
		LoginUser user = ThreadContext.getLoginUser();
		if (user.getId() <= 0) {
			return;
		}
		user.setLocale(locale);
		dao.createSQLQuery("update " + AC_TB.ac_user + " set locale=? where id=?").setParameter(0, locale).setParameter(1, user.getId()).executeUpdate();
	}

}
